package com.zzkj.util
{
	import com.zzkj.view.form.NumberField;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Form;
	import mx.containers.FormItem;
	import mx.controls.ComboBox;
	import mx.controls.DateField;
	import mx.controls.TextArea;
	import mx.controls.TextInput;
	import mx.core.UIComponent;
	import mx.events.ValidationResultEvent;
	import mx.validators.NumberValidator;
	import mx.validators.Validator;
	
	public class ValidateUtil
	{
		public function ValidateUtil()
		{
		}
		
		public static function createReqValidate(target:UIComponent,label:String):Validator{
				var validate:Validator = new Validator();
				validate.required = true;
				validate.source = target;
				var property:String;
				if(target is TextInput){
					property = 'text';
				}else if(target is TextArea){
					property = 'text';
				}else if(target is ComboBox){
					property = 'text';
				}else{
					property = 'text';
				}
				validate.property = property;
				validate.requiredFieldError = label+" 不能为空";
				return validate;
		}
		private static function createNumberValidate(target:NumberField,label:String):NumberValidator{
			var n:NumberValidator = new NumberValidator();
			n.required = true;
			n.source = target;
			n.property = 'text';
			n.minValue = 1;
			n.requiredFieldError = label + "不能为空";
			n.lowerThanMinError = label + "必须大于0";
			return n;
		}
		private static function createDateValidate(target:DateField):DateFieldValidator{
			var d:DateFieldValidator = new DateFieldValidator();
			d.source = target;
			d.property = "text";
			return d;
		}
		public static function getValidateResult(validateList:ArrayCollection):Boolean{
			var flag:Boolean = true;
				var vary:Array = Validator.validateAll(validateList.source);
				for(var k:int=0;k<vary.length;k++){
					var vr:ValidationResultEvent = vary[k];
					if(vr.type==ValidationResultEvent.INVALID){
						flag = false;
					}
				}
				return flag;
		}
		/***
		 * ay = flowBox.getChildern();
		 * validateList:传进来负责记录validate的集合
		 * */
		public static function addValidator(ay:Array,validateList:ArrayCollection):ArrayCollection{
				validateList.removeAll();
				for(var i:Number=0;i<ay.length;i++){
					var obj:Object = ay[i] as Object;
					if(obj is Form){
						var fm:Form = Form(obj);
						var ayf:Array = fm.getChildren();
						for(var j:Number=0;j<ayf.length;j++){
							if(ayf[j] is FormItem){
								var fi:FormItem = ayf[j] as FormItem;
								var ch:Array = fi.getChildren();
								trace(fi.required);
								if(fi.required){
									var v:Validator = null ;
									if(ch[0] is NumberField){
										v = createNumberValidate(ch[0],fi.label);
									}else if(ch[0] is TextArea){
										v = createReqValidate(ch[0],fi.label);
									}else if(ch[0] is ComboBox){
										v = createReqValidate(ch[0],fi.label);
									}else if(ch[0] is DateField){
										v = createDateValidate(ch[0] as DateField);
									}else if(ch[0] is TextInput){
										v = createReqValidate(ch[0],fi.label);
									}
									if(v!=null)
										validateList.addItem(v);
								}
								trace(validateList.length);
							}
						}
						
					}
				}
				return validateList;
			}
		
		
		

	}
}